#include <iostream>
using namespace std;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;

  for (cin >> T; T; T--) {
    int n, m, ans;
    int lasta = 0, lastb = 0;

    cin >> n >> m;
    ans = m;
    for (int i = 0; i < n; i++) {
      int a, b;

      cin >> a >> b;
      if (((a - lasta) & 1) != (b ^ lastb)) {
        ans--;
      }
      lasta = a;
      lastb = b;
    }
    cout << ans << "\n";
  }
  return 0;
}